package com.ruoyi.wms.service.impl;

import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.MD5Utils;
import com.ruoyi.common.core.utils.OrderNumberGenerator;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.wms.domain.BusinessLicenseInfo;
import com.ruoyi.wms.domain.EndLifeVehicleBase;
import com.ruoyi.wms.domain.QualityInspection;
import com.ruoyi.wms.domain.WmsOrder;
import com.ruoyi.wms.dto.WmsOrderDto;
import com.ruoyi.wms.mapper.BusinessLicenseInfoMapper;
import com.ruoyi.wms.mapper.WmsOrderMapper;
import com.ruoyi.wms.service.IQualityInspectionService;
import com.ruoyi.wms.service.IWmsOrderService;
import com.ruoyi.wms.vo.WmsOrderVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * WMS订单Service业务层处理
 *
 * @author ruoyi
 * @date 2025-09-12
 */
@Service
public class WmsOrderServiceImpl implements IWmsOrderService {
    @Autowired
    private WmsOrderMapper wmsOrderMapper;

    @Autowired
    private BusinessLicenseInfoMapper businessLicenseInfoMapper;


    @Autowired
    private IQualityInspectionService qualityInspectionService;

    /**
     * 查询WMS订单
     *
     * @param id WMS订单主键
     * @return WMS订单
     */
    @Override
    public WmsOrderVo selectWmsOrderById(String id) {
        WmsOrderVo wmsOrder = wmsOrderMapper.selectWmsOrderById(id);

        if (!wmsOrder.getEndLifeVehicleList().isEmpty()) {
            EndLifeVehicleBase endLifeVehicleBase = wmsOrder.getEndLifeVehicleList().get(0);
            if (StringUtils.isEmpty(endLifeVehicleBase.getId())) {
                wmsOrder.setEndLifeVehicleList(null);
            }
        }
        return wmsOrder;
    }

    /**
     * 查询WMS订单列表
     *
     * @param wmsOrder WMS订单
     * @return WMS订单
     */
    @Override
    public List<WmsOrderVo> selectWmsOrderList(WmsOrderDto wmsOrder) {
        List<WmsOrderVo> wmsOrderVos = wmsOrderMapper.selectWmsOrderList(wmsOrder);
        for (WmsOrderVo vo : wmsOrderVos) {
            vo.setTotalProductWeight(wmsOrderMapper.selectTotalMassByOrderNum(vo.getOrderNum()));
//             如果这个订单所有车的状态都是1了，订单状态也改成1  。 这个时候 就要往质检表里存一分数据 。
            boolean flag = wmsOrderMapper.checkVehicleStatus(vo.getOrderNum());
            if (flag) {
                WmsOrder o = new WmsOrder();
                o.setId(vo.getId());
                o.setOrderStatus("1");
                wmsOrderMapper.updateWmsOrder(o);
                List<EndLifeVehicleBase> endLifeVehicleList = vo.getEndLifeVehicleList();

                for (EndLifeVehicleBase base : endLifeVehicleList) {
                    QualityInspection qi = new QualityInspection();
                    BeanUtils.copyProperties(base, qi);
//                   如果表里没有才要插入
                    QualityInspection qualityInspection = qualityInspectionService.selectQualityInspectionById(qi.getPlateNo());
                    if (qualityInspection == null) {
                        qualityInspectionService.insertQualityInspection(qi);
                    }
                }
            }
//            质检表里面有就增加，没有就 不增加
        }
        return wmsOrderVos;
    }

    /**
     * 新增WMS订单
     *
     * @param wmsOrder WMS订单
     * @return 结果
     */
    @Override
    public int insertWmsOrder(WmsOrderVo wmsOrder) {
        if (StringUtils.isEmpty(wmsOrder.getOrderNum())) {
            wmsOrder.setOrderNum(OrderNumberGenerator.generateOrderNumber());
        }

        wmsOrder.setId(MD5Utils.md5(wmsOrder.getOrderNum()));
        wmsOrder.setCreateUser(SecurityUtils.getUsername());
        wmsOrder.setCreateDate(DateUtils.getCurrentTime());
        wmsOrder.setUpdateDate(DateUtils.getCurrentTime());
        return wmsOrderMapper.insertWmsOrder(wmsOrder);
    }


    @Override
    public int updateWmsOrder(WmsOrderVo wmsOrderVo) {
        BusinessLicenseInfo businessLicenseInfo = wmsOrderVo.getBusinessLicenseInfo();

        if (businessLicenseInfo == null || StringUtils.isEmpty(businessLicenseInfo.getId())) {
//            新增供应商
            String businessLicenseId = MD5Utils.md5(businessLicenseInfo.getCompanyCode() + businessLicenseInfo.getSubjectRole() + businessLicenseInfo.getSubjectRole() + businessLicenseInfo.getDataSource());
            if (StringUtils.isNotEmpty(wmsOrderVo.getBusinessLicenseId())) {
                businessLicenseInfo.setId(wmsOrderVo.getBusinessLicenseId());

            } else {
                businessLicenseInfo.setId(businessLicenseId);
            }
            insertBusinessLicenseInfo(businessLicenseInfo);

        } else {
//            修改供应商
            businessLicenseInfoMapper.updateLvBusinessLicenseInfo(businessLicenseInfo);
        }


        wmsOrderVo.setBusinessLicenseId(businessLicenseInfo.getId());
        int updateResult = 0;
        if (StringUtils.isEmpty(wmsOrderVo.getId())) {
            updateResult = insertWmsOrder(wmsOrderVo);
        }
        wmsOrderVo.setUpdateDate(DateUtils.getCurrentTime());
        updateResult = wmsOrderMapper.updateWmsOrder(wmsOrderVo);
        if (0 == updateResult) {
            updateResult = insertWmsOrder(wmsOrderVo);
        }

        return updateResult;


    }


    public int insertBusinessLicenseInfo(BusinessLicenseInfo businessLicenseInfo) {


        businessLicenseInfo.setCreateUser(SecurityUtils.getUsername());
        businessLicenseInfo.setCreateDate(DateUtils.getCurrentTime());
        if (StringUtils.isEmpty(businessLicenseInfo.getSubjectRole())) {
            businessLicenseInfo.setSubjectRole("供应商(自然人)");

        }
        if (StringUtils.isEmpty(businessLicenseInfo.getSubjectType())) {
            businessLicenseInfo.setSubjectType("自然人");

        }
        return businessLicenseInfoMapper.insertBusinessLicenseInfo(businessLicenseInfo);
    }


    /**
     * 批量删除WMS订单
     *
     * @param ids 需要删除的WMS订单主键
     * @return 结果
     */
    @Override
    public int deleteWmsOrderByIds(String[] ids) {
        return wmsOrderMapper.deleteWmsOrderByIds(ids);
    }

    /**
     * 删除WMS订单信息
     *
     * @param id WMS订单主键
     * @return 结果
     */
    @Override
    public int deleteWmsOrderById(String id) {
        return wmsOrderMapper.deleteWmsOrderById(id);
    }
}
